package com.zhilianbao.leyaogo.ui.fragment.goodscategory;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.bql.baseadapter.recycleView.QuickRcvAdapter;
import com.bql.statetypelayout.AnimationStateTypeLayout;
import com.melnykov.fab.FloatingActionButton;
import com.zhilianbao.leyaogo.R;
import com.zhilianbao.leyaogo.ui.adapter.goodscategory.DetailsAdapter;
import com.zhilianbao.leyaogo.ui.fragment.base.RefreshAndLoadFragment;
import com.zhilianbao.leyaogo.utils.DensityUtils;
import com.zhilianbao.leyaogo.utils.LoadStateUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;


/**
 * 商品信息页面上拉出来的底部的页面
 * Created by gyt on 2016/11/7
 */
public class GoodsInfoBottomFragment extends RefreshAndLoadFragment<String> {

    @BindView(R.id.fab_go_top)
    FloatingActionButton     mFabGoTop;
    @BindView(R.id.stateLayout)
    AnimationStateTypeLayout mStateLayout;
    private OnFabClickListener mOnFabClickListener;

    private static final String ARG_URL = "ARG_URL";
    private List<String>   mGoodsPics;
    private DetailsAdapter mDetailsAdapter;
    private LoadStateUtils mLoadStateUtils;


    public static GoodsInfoBottomFragment getInstance(List<String> goodsPics) {
        GoodsInfoBottomFragment fragment = new GoodsInfoBottomFragment();
        Bundle bundle = new Bundle();
        bundle.putStringArrayList(ARG_URL, (ArrayList) goodsPics);
        fragment.setArguments(bundle);
        return fragment;
    }


    public void setOnFabClickListener(OnFabClickListener listener) {
        this.mOnFabClickListener = listener;
    }

    @Override
    public void onRcvItemClick(RecyclerView.ViewHolder holder, int position) {

    }

    @Override
    public QuickRcvAdapter<String> getAdapter() {
        return mDetailsAdapter;
    }

    @Override
    public RecyclerView.ItemDecoration getItemDecoration() {
        return null;
    }

    @Override
    public RecyclerView.LayoutManager getLayoutManager() {
        return new LinearLayoutManager(mActivity);
    }

    @Override
    public void onRefresh() {

    }

    @Override
    public void loadDataList(int curPage, boolean isPullToRefresh) {
        //        performLoadDataList();
        if (mGoodsPics == null || mGoodsPics.size() == 0) {
            mLoadStateUtils.showEmpty(getResources().getString(R.string.hint_no_goods_details_pic));
        } else {
            mDetailsAdapter.clear();
            mDetailsAdapter.addToLast(mGoodsPics);
        }
    }

    @Override
    public void onLoadMore() {

    }

    @Override
    public boolean canRefresh() {
        return false;
    }

    /**
     * fab点击的回调
     */
    public interface OnFabClickListener {
        void onFabClick(View view);
    }


    @OnClick(R.id.fab_go_top)
    public void onClick(View view) {
        if (mOnFabClickListener != null) {
            mOnFabClickListener.onFabClick(view);
        }
    }

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.fragment_goods_info_bottom;
    }

    @Override
    protected void initViewsAndEvents(Bundle savedInstanceState) {
        Bundle bundle = getArguments();
        if (bundle != null) {
            mGoodsPics = bundle.getStringArrayList(ARG_URL);
        }

        mLoadStateUtils = new LoadStateUtils(mActivity, getView());
        mDetailsAdapter = new DetailsAdapter(mActivity, mList);
        mRcvLoadMore.setOnScrollListener(new RecyclerView.OnScrollListener() {
            private int totalDy = 0;

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                totalDy += dy;
                if (totalDy >= DensityUtils.dp2px(mActivity, (float) 200)) {
                    mFabGoTop.setVisibility(View.VISIBLE);
                    mFabGoTop.show(true);
                } else {
                    mFabGoTop.hide(true);
                }
            }
        });
    }

    @Override
    protected void initToolbarHere() {

    }

}
